#include "ZTypes.h"
#include "Types.h"
#include "eeprom.h"
#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include <stdio.h>
#include "ThreadControl.h"

history_settings_t curHistorySettings;

/*
 * Initialize the history logging
 *
 * Params:
 *		none
 *
 * Returns:
 *		SUCCESS if initialized correctly
 *		GENERAL_ERROR if initialization fails
 */
error_t initHistory()
{
	curHistorySettings.allTimeLog.ACRuntime = 0;
	curHistorySettings.allTimeLog.HeatRuntime = 0;
	curHistorySettings.allTimeLog.FanRuntime = 0;

	return SUCCESS;
}


/*
 * Public function to add AC run time to running total(s)
 *
 * Params:
 *		time - the duration (in seconds) that the AC ran
 *
 * Returns:
 *		SUCCESS when the time is saved successfuly to EEPROM
 *		GENERAL_ERROR if the time cannot be saved
 */
void logACTime(TIME_t time)
{
	//NOTE: this only handles the all time log right now
	curHistorySettings.allTimeLog.ACRuntime += time;
	printf("Logged %d seconds of AC time\n", time);
}


/*
 * Public function to add heat run time to running total(s)
 *
 * Params:
 *		time - the duration (in seconds) that the heat ran
 *
 * Returns:
 *		SUCCESS when the time is saved successfuly to EEPROM
 *		GENERAL_ERROR if the time cannot be saved
 */
void logHeatTime(TIME_t time)
{
	//NOTE: this only handles the all time log right now
	curHistorySettings.allTimeLog.HeatRuntime += time;
	printf("Logged %d seconds of Heat time\n", time);
}


/*
 * Public function to add Fan run time to running total(s)
 *
 * Params:
 *		time - the duration (in seconds) that the fan ran
 *
 * Returns:
 *		SUCCESS when the time is saved successfuly to EEPROM
 *		GENERAL_ERROR if the time cannot be saved
 */
void logFanTime(TIME_t time)
{
	//NOTE: this only handles the all time log right now
	curHistorySettings.allTimeLog.FanRuntime += time;
	printf("Logged %d seconds of Fan time\n", time);
}


/*
 * Public function to fetch the running history logs for AC, Heat and Fan
 *
 * Params:
 *		duration - the time slice of the log to report (DAY, WEEK, MONTH, YEAR, ALL_TIME)
 *
 * Returns:
 *		The history_log_t containing the results for that duration
 */
history_log_t getHistoryLog(duration_t duration)
{
	// NOTE: Only ALL_TIME log is implemented right now
	if(duration == ALL_TIME)
	{
		return curHistorySettings.allTimeLog;
	}
	else
	{
		return curHistorySettings.allTimeLog;;
	}
}

/*
 * Public function to clear the history logs (back to 0)
 *
 * Params:
 *		none
 *
 * Returns:
 *		none
 */
error_t resetHistory()
{
	//Note: Only ALL_TIME is handled
	curHistorySettings.allTimeLog.ACRuntime = 0;
	curHistorySettings.allTimeLog.HeatRuntime = 0;
	curHistorySettings.allTimeLog.FanRuntime = 0;
	return SUCCESS;
}

